////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2008 Michiel Helvensteijn                                    //
//                                                                            //
// This file is part of the Mist compiler.                                    //
//                                                                            //
// The Mist compiler is free software: you can redistribute it and/or modify  //
// it under the terms of the GNU General Public License as published by       //
// the Free Software Foundation, either version 3 of the License, or          //
// (at your option) any later version.                                        //
//                                                                            //
// The Mist compiler is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// along with the Mist compiler.  If not, see <http://www.gnu.org/licenses/>. //
////////////////////////////////////////////////////////////////////////////////

#include "visitors/sideeffectvisitor.hpp"

/******************************************************************************
 * Includes                                                                   *
 ******************************************************************************/

#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH

#include <boost/format.hpp>
using boost::format;

#include <vector>
using std::vector;

#include <string>
using std::string;

#include <boost/shared_ptr.hpp>
using boost::dynamic_pointer_cast;

#include <typeinfo>

#include "ast/nodetypes.hpp"
#include "visitors/visitor.hpp"
#include "visitors/echovisitor.hpp"
#include "visitors/expressionnavigator.hpp"
#include "messages.hpp"
#include "common.hpp"
#include "assert.hpp"
#include "symboltree.hpp"

/******************************************************************************
 * SideEffectVisitor Class                                                    *
 ******************************************************************************/

class SideEffectVisitor : public ExpressionNavigator {
	public:
		
		SideEffectVisitor(shared_ptr<Messages> messages) : ExpressionNavigator(messages) {}
		
		void visit(shared_ptr<ArithmeticOperation>   node);
		void visit(shared_ptr<FunctionCall>          node);
		void visit(shared_ptr<UnaryOperation>        node);
		void visit(shared_ptr<IntegerLiteral>        node);
		void visit(shared_ptr<BooleanLiteral>        node);
		void visit(shared_ptr<Null>                  node);
		void visit(shared_ptr<Assertion>             node);
		void visit(shared_ptr<Assignment>            node);
		void visit(shared_ptr<IdAssignment>          node);
		void visit(shared_ptr<Subscripting>          node);
		void visit(shared_ptr<ComparisonChain>       node);
		void visit(shared_ptr<AndChain>              node);
		void visit(shared_ptr<OrChain>               node);
		void visit(shared_ptr<ImpliesChain>          node);
		void visit(shared_ptr<Range>                 node);
		void visit(shared_ptr<Reference>             node);
		void visit(shared_ptr<BooleanPointerLiteral> node);
		void visit(shared_ptr<IntegerPointerLiteral> node);
};

/******************************************************************************
 * Function Implementations                                                   *
 ******************************************************************************/

void find_side_effects(shared_ptr<Node> node, shared_ptr<Messages> messages) {
	assert(node);
	
	shared_ptr<SideEffectVisitor> visitor(new SideEffectVisitor(messages));
	
	node->accept(visitor);
}

/******************************************************************************/

void SideEffectVisitor::visit(shared_ptr<ArithmeticOperation> node) {
	assert(node);
	
	ExpressionNavigator::visit(node);
	
	node->set_side_effects(node->left_operand()->side_effects() || node->right_operand()->side_effects());
}

void SideEffectVisitor::visit(shared_ptr<FunctionCall> node) {
	assert(node);
	
	ExpressionNavigator::visit(node);
	
	bool sideEffectFound = false;
	
	for (unsigned i = 0; i < node->parameter_count() && !sideEffectFound; ++i)
		sideEffectFound |= node->parameters()[i]->side_effects();
	
	assert(typeid(*node->base()) == typeid(Reference));
	
	shared_ptr<Reference> base = dynamic_pointer_cast<Reference>(node->base());
	
	if (typeid(*base->type()) == typeid(FunctionType)) {
		shared_ptr<FunctionType> ftype = dynamic_pointer_cast<FunctionType>(base->type());
		foreach (ParameterType ptype, ftype->parameter_types())
			sideEffectFound |= ptype.policy->identityPassed;
	}
	
	if (base->symbol_is_set())
		sideEffectFound |= !base->symbol()->written_vars_recursive().empty();
	
	node->set_side_effects(sideEffectFound);
}

void SideEffectVisitor::visit(shared_ptr<UnaryOperation> node) {
	assert(node);
	
	ExpressionNavigator::visit(node);
	
	node->set_side_effects(node->operand()->side_effects());
}

void SideEffectVisitor::visit(shared_ptr<IntegerLiteral> node) {
	assert(node);
	
	node->set_side_effects(false);
}

void SideEffectVisitor::visit(shared_ptr<BooleanLiteral> node) {
	assert(node);
	
	node->set_side_effects(false);
}

void SideEffectVisitor::visit(shared_ptr<Null> node) {
	assert(node);
	
	node->set_side_effects(false);
}

void SideEffectVisitor::visit(shared_ptr<Assertion> node) {
	assert(node);
	
	ExpressionNavigator::visit(node);
	
	if (node->condition_is_set()) {
		if (node->condition()->side_effects()) {
			messages()->add("side-effect-error", node->condition()->location())
				% "the assertion condition";
		}
	} else {
		foreach (VarDec var, node->variable_declaration()->variables()) {
			if (var.is_initialized() && var.init->side_effects()) {
				messages()->add("side-effect-error", node->condition()->location())
					% format("the initialization of the logical var `%1%'")
						% var.name->text();
			}
		}
	}
}

void SideEffectVisitor::visit(shared_ptr<Assignment> node) {
	assert(node);
	
	ExpressionNavigator::visit(node);
	
	node->set_side_effects(true);
}

void SideEffectVisitor::visit(shared_ptr<IdAssignment> node) {
	assert(node);
	
	ExpressionNavigator::visit(node);
	
	node->set_side_effects(true);
}

void SideEffectVisitor::visit(shared_ptr<Subscripting> node) {
	assert(node);
	
	ExpressionNavigator::visit(node);
	
	node->set_side_effects(node->base()->side_effects() || node->subscript()->side_effects());
}

void SideEffectVisitor::visit(shared_ptr<ComparisonChain> node) {
	assert(node);
	
	ExpressionNavigator::visit(node);
	
	bool sideEffectFound = false;
	
	for (unsigned i = 0; i < node->operand_count() && !sideEffectFound; ++i)
		sideEffectFound |= node->operands()[i]->side_effects();
	
	node->set_side_effects(sideEffectFound);
}

void SideEffectVisitor::visit(shared_ptr<AndChain> node) {
	assert(node);
	
	ExpressionNavigator::visit(node);
	
	bool sideEffectFound = false;
	
	for (unsigned i = 0; i < node->operand_count() && !sideEffectFound; ++i)
		sideEffectFound |= node->operands()[i]->side_effects();
	
	node->set_side_effects(sideEffectFound);
}

void SideEffectVisitor::visit(shared_ptr<OrChain> node) {
	assert(node);
	
	ExpressionNavigator::visit(node);
	
	bool sideEffectFound = false;
	
	for (unsigned i = 0; i < node->operand_count() && !sideEffectFound; ++i)
		sideEffectFound |= node->operands()[i]->side_effects();
	
	node->set_side_effects(sideEffectFound);
}

void SideEffectVisitor::visit(shared_ptr<ImpliesChain> node) {
	assert(node);
	
	ExpressionNavigator::visit(node);
	
	bool sideEffectFound = false;
	
	for (unsigned i = 0; i < node->operand_count() && !sideEffectFound; ++i)
		sideEffectFound |= node->operands()[i]->side_effects();
	
	node->set_side_effects(sideEffectFound);
}

void SideEffectVisitor::visit(shared_ptr<Range> node) {
	assert(node);
	
	ExpressionNavigator::visit(node);
	
	node->set_side_effects(node->lower_bound()->side_effects() || node->upper_bound()->side_effects() || node->step()->side_effects());
}

void SideEffectVisitor::visit(shared_ptr<Reference> node) {
	assert(node);
	
	node->set_side_effects(false);
}

void SideEffectVisitor::visit(shared_ptr<BooleanPointerLiteral> node) {
	assert(node);
	
	ExpressionNavigator::visit(node);
	
	node->set_side_effects(node->value()->side_effects());
}

void SideEffectVisitor::visit(shared_ptr<IntegerPointerLiteral> node) {
	assert(node);
	
	ExpressionNavigator::visit(node);
	
	node->set_side_effects(node->value()->side_effects());
}
